﻿Public Class FlipControl
    Inherits ContentControl

    Shared Sub New()
        DefaultStyleKeyProperty.OverrideMetadata(GetType(FlipControl), New FrameworkPropertyMetadata(GetType(FlipControl)))
    End Sub



    Public Shared ReadOnly FlipCommand As ICommand = New RoutedUICommand("Flip", "Flip", GetType(FlipControl))
    Public Shared ReadOnly FlipBackCommand As ICommand = New RoutedUICommand("Flip Back", "FlipBack", GetType(FlipControl))



    Public Shared ReadOnly IsFlippedProperty As DependencyProperty = _
        DependencyProperty.Register( _
            "IsFlipped", GetType(Boolean), GetType(FlipControl), _
            New FrameworkPropertyMetadata(False))

    Public Property IsFlipped As Boolean
        Get
            Return CType(GetValue(IsFlippedProperty), Boolean)
        End Get
        Set(ByVal value As Boolean)
            SetValue(IsFlippedProperty, value)
        End Set
    End Property



    Public Shared ReadOnly FlipContentProperty As DependencyProperty = _
        DependencyProperty.Register( _
            "FlipContent", GetType(Object), GetType(FlipControl), _
            New FrameworkPropertyMetadata( _
                Nothing, _
                Sub(d, e) CType(d, FlipControl).OnFlipContentChanged(CType(e.OldValue, Object), CType(e.NewValue, Object))))

    Public Property FlipContent As Object
        Get
            Return CType(GetValue(FlipContentProperty), Object)
        End Get
        Set(ByVal value As Object)
            SetValue(FlipContentProperty, value)
        End Set
    End Property

    Private Sub OnFlipContentChanged(oldValue As Object, newValue As Object)
        If oldValue IsNot Nothing AndAlso TypeOf oldValue Is UIElement Then
            RemoveLogicalChild(oldValue)
        End If
        If newValue IsNot Nothing AndAlso TypeOf newValue Is UIElement Then
            AddLogicalChild(newValue)
        End If
    End Sub

End Class
